/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hanze.shoestring.dao.impl;

import java.util.List;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.*;
import nl.hanze.shoestring.dao.MedewerkerDAO;
import nl.hanze.shoestring.domain.Medewerker;
import nl.hanze.shoestring.domain.Medewerker_;
import nl.hanze.shoestring.domain.Persoon;
import nl.hanze.shoestring.domain.Persoon_;

/**
 *
 * @author alex
 */
public class MedewerkerDAOImpl extends BaseDAOImpl<Medewerker> implements MedewerkerDAO{

    public MedewerkerDAOImpl() {
    }

    @Override
    public List<Medewerker> findByGids(Boolean isGids) {
        open();
        Query query = em.createQuery("Select m FROM Medewerker m WHERE m.gids = :gids");
        query.setParameter("gids", isGids);
        return query.getResultList(); 
    }

    @Override
    public List<Medewerker> find(String name) {
                boolean hasName = name != null && name.length() > 0;
        
        if (!hasName) {
            return getAll();
        }

        open();
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Medewerker> query = cb.createQuery(Medewerker.class);
        Root<Medewerker> medewerker  = query.from(Medewerker.class);
        Join<Medewerker, Persoon> persoon = medewerker.join(Medewerker_.persoon);
        

        Predicate p = cb.or(cb.like(persoon.get(Persoon_.achternaam), name + "%"),cb.like(persoon.get(Persoon_.voornaam), name + "%"), 
                cb.like(persoon.get(Persoon_.email), "%" + name + "%"));               

        query.where(p);

        TypedQuery<Medewerker> typedQuery = em.createQuery(query);
        return typedQuery.getResultList();
    }
    
}
